<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_cinema extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		return true;
		
	}
	public function create_cinema($data){
		
			$file = $data['cinema_image']['tmp_name'];
			$filename = time().$data["cinema_image"]['name'];
			$cinema_image_path="uploads/cinema/".$filename;
			move_uploaded_file($file, $banner_image_path);
			
	$query="insert into `cinema` (cinema_title,cinema_date,cinema_content,cinema_author,cinema_image,created_date,updated_date) VALUES ('".$data['cinema_title']."','".$data['cinema_date']."','".$data['cinema_content']."','".$data['cinema_author']."','".$this->conn->baseurl().$cinema_image_path."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);	
		if($result){
			return array('message'=>'Cinema/Lifestyle successfully created');			
		}else{
			return array('message'=>'Cinema/Lifestyle Creation Failed');
		}	
	}
	public function edit_cinema($data)
	{	
		if($data['cinema_image']!="")
			{
			$file = $data['cinema_image']['tmp_name'];
			$filename = time().$data["cinema_image"]['name'];
			$cinema_image_path="uploads/cinema/".$filename;
			move_uploaded_file($file, $banner_image_path);
			}
			else
			{
				$cinema_image_path = "";
			}
		
	$query="update `cinema` set cinema_title='".$data['cinema_title']."',cinema_date='".$data['cinema_date']."',cinema_content='".$data['cinema_content']."',cinema_author='".$data['cinema_author']."'".($cinema_image_path!="" ? ',banner_image='.$this->conn->baseurl().$cinema_image_path : '').",status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where cinema_id=".$data['cinema_id'];
		//print_r($query);exit;
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Cinema/Lifestyle Updated Successfully');			
		}else{
			return array('message'=>'Cinema/Lifestyle Updates Failed');
		}	
	
	}
	public function get_cinema($data)
	{	
		$query="SELECT * from cinema where cinema_id=".$data['cinema_id'];
			
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Cinema/Lifestyle get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Cinema/Lifestyle get Failed');
		}	
		
	}
	public function delete_cinema($data){
		$data=$this->conn->dbCommand("update `cinema` set status='".$data['status']."' where cinema_id=".$data['cinema_id']);
		if($data)
			return array('message'=>"Cinema/Lifestyle Deleted Successfully");		
		else
			return array('message'=>'Cinema/Lifestyle Deletion Fails');
	}
	public function all_cinemas($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select * from `cinema`";
	$query_string1="SELECT count(*) as total from cinema";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
			
		}else if($data['search_by'] == 'cinema_title')
		{
			
			$search_by = "cinema_title";
			
		}else if($data['search_by'] == 'cinema_author')
		{
			$search_by = "cinema_author";
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Top stories","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Cinemas');
	}

	
	

}
?>